<template>
  <div class="pictureField">
    <template v-if="itemdata.lMustInput === '0'">
      <div style="padding: 10px 10px;">{{ itemdata.sTitle }}</div>
      <input type="hidden" v-model="itemdata.valuehidden" />
      <input type="hidden" v-model="itemdata.value" />
      <div class="L-upload-block">
        <van-uploader v-if="itemdata.sFormat == 'refer:only-camera'" placeholder="请上传图片" v-model="itemdata.valuelist" capture="camera" :after-read="onImageHead" @delete="delImageHead" accept="image/*" multiple :name="itemdata.sField">
        </van-uploader>
        <van-uploader v-else placeholder="请上传图片" v-model="itemdata.valuelist" :after-read="onImageHead" @delete="delImageHead" accept="image/*" multiple :name="itemdata.sField">
        </van-uploader>
      </div>

    </template>
    <template v-if="itemdata.lMustInput === '1'">
      <div style="padding: 10px 0;"><span style="color:red">*</span>{{ itemdata.sTitle }}</div>
      <input type="hidden" v-model="itemdata.valuehidden" />
      <input type="hidden" v-model="itemdata.value" />
      <van-uploader v-if="itemdata.sFormat == 'refer:only-camera'" placeholder="请上传图片" v-model="itemdata.valuelist" capture="camera" :after-read="onImageHead" @delete="delImageHead" accept="image/*" multiple :name="itemdata.sField">
      </van-uploader>
      <van-uploader v-else placeholder="请上传图片" v-model="itemdata.valuelist" :after-read="onImageHead" @delete="delImageHead" accept="image/*" multiple :name="itemdata.sField">
      </van-uploader>
    </template>
  </div>
</template>
<script>
import { Toast } from 'vant'
import Vue from 'vue'
Vue.use(Toast)
export default {
  data () {
    return {
      valuelist: null
    }
  },
  props: {
    jsondata: {
      type: Object,
      required: false
    },
    itemdata: {
      type: Object,
      required: false
    },
    ishead: {
      type: Boolean,
      default: false
    },
    index: {
      type: Number,
      required: false
    }
  },
  mounted () {
    //console.log('this.valuelist', this.itemdata.valuelist)
    if (this.itemdata.valuelist.length) {
      // this.valuelist = JSON.parse(this.itemdata.valuelist)
    }
  },
  methods: {
    onImageHead (file, detail) {
      // 上传
      let deviceFile = []
      let formData = new FormData()
      if (Array.isArray(file)) {
        deviceFile = file
      } else {
        deviceFile.push(file)
      }
      for (let i = 0; i < deviceFile.length; i++) {
        formData.append('image' + i, deviceFile[i].file, deviceFile[i].name)
      }
      const toast = Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '等待上传中...请稍候'
      })
      this.axios({
        method: 'post',
        url: '/api/upload.imagedo',
        data: formData,
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(res => {
        // 清掉提示
        toast.clear()
        if (res.data.flag === '0') {
          let fileInfo = res.data.fileInfo
          if (this.ishead) {
            for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
              let headData = this.jsondata.head[headIdx].groupvalue
              for (let i = 0; i < headData.length; i++) {
                if (headData[i].sField === detail.name) {
                  headData[i].valuehidden = fileInfo[0].newname + ';' + (headData[i].valuehidden && headData[i].valuehidden != null && headData[i].valuehidden != undefined && headData[i].valuehidden != 'undefined' ? headData[i].valuehidden : '')
                  headData[i].value = fileInfo[0].oldname + ';' + (headData[i].value && headData[i].value != null && headData[i].value != undefined && headData[i].value != 'undefined' ? headData[i].value : '')
                }
              }
            }
          } else {
            // 目前只处理单子表（第一个子表）
            for (let key in this.jsondata.bodydata) {
              let headData = this.jsondata.bodydata[key][this.index]
              for (let i = 0; i < headData.length; i++) {
                if (headData[i].sField === detail.name) {
                  headData[i].valuehidden = fileInfo[0].newname + ';' + (headData[i].valuehidden && headData[i].valuehidden != null && headData[i].valuehidden != undefined && headData[i].valuehidden != 'undefined' ? headData[i].valuehidden : '')
                  headData[i].value = fileInfo[0].oldname + ';' + (headData[i].value && headData[i].value != null && headData[i].value != undefined && headData[i].value != 'undefined' ? headData[i].value : '')
                }
              }
              break
            }
          }
          // Toast('上传成功')
        } else {
          Toast('上传失败')
        }
      }).catch(res => {
        //console.log(res)
      })
      // let headdata = this.jsondata.head[0].groupvalue
      //console.log(file)
      //console.log(detail)
    },
    delImageHead (file, detail) {
      if (this.ishead) {
        for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
          let headData = this.jsondata.head[headIdx].groupvalue
          for (let i = 0; i < headData.length; i++) {
            if (headData[i].sField === detail.name) {
              let fileVal = headData[i].valuehidden.split(';')
              let fileTitle = headData[i].value.split(';')
              for (let j = 0; j < fileTitle.length; j++) {
                if (fileTitle[j] === file.file.name) {
                  fileVal[j] = ''
                  fileTitle[j] = ''
                }
              }
              let fileVal1 = ''
              let fileTitle1 = ''
              for (let j = 0; j < fileTitle.length; j++) {
                if (fileTitle[j] !== '') {
                  fileVal1 += fileVal[j] + ';'
                  fileTitle1 += fileTitle[j] + ';'
                }
              }
              headData[i].valuehidden = fileVal1
              headData[i].value = fileTitle1
            }
          }
        }
      } else {
        for (let key in this.jsondata.bodydata) {
          let headData = this.jsondata.bodydata[key][this.index]
          for (let i = 0; i < headData.length; i++) {
            if (headData[i].sField === detail.name) {
              let fileVal = headData[i].valuehidden.split(';')
              let fileTitle = headData[i].value.split(';')
              for (let j = 0; j < fileTitle.length; j++) {
                if (fileTitle[j] === file.file.name) {
                  fileVal[j] = ''
                  fileTitle[j] = ''
                }
              }
              let fileVal1 = ''
              let fileTitle1 = ''
              for (let j = 0; j < fileTitle.length; j++) {
                if (fileTitle[j] !== '') {
                  fileVal1 += fileVal[j] + ';'
                  fileTitle1 += fileTitle[j] + ';'
                }
              }
              headData[i].valuehidden = fileVal1
              headData[i].value = fileTitle1
            }
          }
          break
        }
      }
      // Toast('删除成功')
    }
  }
}
</script>

<style scoped lang="less">
.L-upload-block {
  padding: 10px;
}

.pictureField {
  // padding: 10px 16px;
  padding: 10px 5px;
  border-bottom: 1px solid #eee;
  background: #fff;

  color: #646566;
  line-height: 16px;
  font-size: 16px;
  font-weight: 400;
  // background: #F6F6F6;
  // margin: 10px auto;
}
</style>
